﻿
//  Copyright (c) Microsoft Corporation.  All Rights Reserved.

using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using Sripirom.ATS.Core.Logging;


namespace Sripirom.ATS.Core.ServiceModel.Description
{ 
    public class AtsServiceBehavior : IServiceBehavior
    {
        private ServiceCommandsCollection _operationCollection;
        public AtsServiceBehavior(ServiceCommandsCollection operationsCollection)
        {
           LoggerFactory.CreateLog().LogInfo("SchemaValidationBehavior.Constructor");
            _operationCollection = operationsCollection;
        }
   
        public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
        {
            LoggerFactory.CreateLog().LogInfo("-ServiceOperationBehavior.AddBindingParameters");
            
        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            LoggerFactory.CreateLog().LogInfo("I:ServiceOperationBehavior ApplyDispatchBehavior " + serviceDescription.Name);

            foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
            {
               LoggerFactory.CreateLog().LogInfo("I:Loop Endpoint " + endpoint.Name + " " + endpoint.Address.ToString());
                foreach (OperationDescription operation in endpoint.Contract.Operations)
                {
                   LoggerFactory.CreateLog().LogInfo("I:Loop Operation " + operation.Name);
                   if (operation.Name != "Get") {
                       IOperationBehavior behavior = new AtsOperationBehavior(_operationCollection[operation.Name]);
                     
                       operation.Behaviors.Add(behavior);
                   }  
                  
                    
                   LoggerFactory.CreateLog().LogInfo("O:Loop Operation " + operation.Name);
                }
              LoggerFactory.CreateLog().LogInfo("O:Loop Endpoint " + endpoint.Name);
            }
           LoggerFactory.CreateLog().LogInfo("O:ServiceOperationBehavior ApplyDispatchBehavior");
        }

        public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
           LoggerFactory.CreateLog().LogInfo("-ServiceOperationBehavior.Validate");
            
        }
    }
}
